import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:convert';

class QueueFirst extends StatefulWidget {
  QueueFirst({Key key}) : super(key: key);

  @override
  _QueueFirstState createState() => _QueueFirstState();
}

class _QueueFirstState extends State<QueueFirst> {
  String _queueId;
  Map _queueInfo;
  List _queueStatusList = ['/second', '/third', '/fourth'];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getResultIdAndInfo();
  }

  _getResultIdAndInfo() async {
    var apiUrl = 'http://172.20.10.9:5758/app/queue/queryLogisticsInfo';
    Map jsonData = {"vno": "冀D56820", "driverPhone": "18810230685"};
    Response response = await Dio().post(apiUrl, data: jsonData);
    setState(() {
      _queueId = response.data['result'];
    });
  }

  _getQueueInfo() async {
    var apiUrl =
        'http://192.168.0.103:5758/app/queue/queryQueueInfo'; // 需要更换成本地ip
    Map jsonData = {"id": _queueId};
    Response responseInfo = await Dio().post(apiUrl, data: jsonData);
    // print(responseInfo.data);
    _queueInfo = responseInfo.data['result'];
    print(_queueInfo);
    // print(_queueInfo['queueStatus'] is int);
    int queueStatus = _queueInfo['queueStatus'];
    String str;
    if (queueStatus != 0 && queueStatus != 4) {
      str = _queueStatusList[queueStatus - 1];
      print(str);

      // Navigator.pushNamed(context, str, arguments: _queueInfo);
      Navigator.pushNamed(context, str, arguments: {
        "vno": _queueInfo['vno'],
        'id': _queueInfo['id'],
        'queueStatus': _queueInfo['queueStatus'],
        'allocateNum': _queueInfo['allocateNum'],
        'waitingNumber': _queueInfo['waitingNumber'],
        'loadGoodsAddr': _queueInfo['loadGoodsAddr'],
        'unloadGoodsAddr': _queueInfo['unloadGoodsAddr']
      });

      // Navigator.of(context).pushReplacementNamed(str);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('排队管理'),
        ),
        body: Container(
          // height: 150,
          child: Column(children: [
            Card(
                child: Padding(
              padding: EdgeInsets.fromLTRB(15, 10, 15, 5),
              child: Column(
                children: [
                  Container(
                      padding: EdgeInsets.fromLTRB(30, 4, 30, 4),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(3.0),
                        color: Color.fromRGBO(128, 212, 248, 1),
                      ),
                      child: Text('!  进入厂区需提前排队取号，请先排队',
                          style: TextStyle(
                            // height: 1.5 / 1,
                            fontSize: 16,
                            color: Colors.white,
                          ))),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Expanded(
                          flex: 2,
                          child: Container(
                            child: Text('装：北京市海淀区第三中路',
                                style: TextStyle(letterSpacing: 1)),
                          )),
                      SizedBox(width: 10),
                      Expanded(
                          flex: 1,
                          child: Container(
                            child: ElevatedButton(
                              child: Text('导航前往'),
                              onPressed: () {},
                              // onPressed: _getResultIdAndInfo,
                            ),
                          )),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Expanded(
                          flex: 2,
                          child: Container(
                            child: Text('卸：北京市海淀区千方大厦a座',
                                style: TextStyle(letterSpacing: 1)),
                          )),
                      SizedBox(width: 10),
                      Expanded(
                          flex: 1,
                          child: Container(
                            child: ElevatedButton(
                              child: Text('导航前往'),
                              onPressed: () {},
                            ),
                          )),
                    ],
                  )
                ],
              ),
            )),
            Padding(
                padding: EdgeInsets.fromLTRB(15, 10, 15, 5),
                child: Column(
                  children: [
                    Container(
                        alignment: Alignment.topLeft,
                        child: Text(
                          '车牌号：冀D56820',
                        )),
                    Container(
                        alignment: Alignment.topLeft,
                        child: Text(
                          '排队状态：排队中',
                        )),
                    Center(
                      child: Container(
                        height: 300,
                        child: Image.asset('images/qrcode.png'),
                      ),
                    ),
                    Center(
                      child: ElevatedButton(
                        child: Text('排队取号'),
                        onPressed: _getQueueInfo,
                      ),
                    ),
                    Text('距离厂区较远，暂不能排队'),
                  ],
                ))
          ]),
        ));
  }
}
